package com.zh.view.component

import androidx.compose.animation.animateContentSize
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.*
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp

enum class SnackbarType {
    INFO, SUCCESS, WARN, ERROR
}

// 2. 实现自定义 SnackbarVisuals
class CustomSnackbarVisuals(
    override val message: String,
    override val actionLabel: String? = null,
    override val withDismissAction: Boolean = true,
    override val duration: SnackbarDuration = SnackbarDuration.Short,
    val type: SnackbarType = SnackbarType.INFO,
    val icon: ImageVector? = null // 可选自定义图标
) : SnackbarVisuals


// 4. 创建自定义 SnackbarHost（使用最新 API）
@Composable
fun CustomSnackbarHost(
    hostState: SnackbarHostState,
    modifier: Modifier = Modifier
) {
    SnackbarHost(
        hostState = hostState,
        modifier = modifier,
        snackbar = { data ->
            val customVisuals = data.visuals as? CustomSnackbarVisuals ?: return@SnackbarHost

            // 根据类型确定颜色
            val (bgColor, icon) = when (customVisuals.type) {
                SnackbarType.INFO -> MaterialTheme.colorScheme.primary to (customVisuals.icon ?: Icons.Default.Info)
                SnackbarType.SUCCESS -> Color(0xFF4CAF50) to (customVisuals.icon ?: Icons.Default.Check)
                SnackbarType.WARN -> Color(0xFFFFC107) to (customVisuals.icon ?: Icons.Default.Warning)
                SnackbarType.ERROR -> Color(0xFFF44336) to (customVisuals.icon ?: Icons.Default.Error)
            }

            // 使用最新 API 的 Snackbar
            Snackbar(
                modifier = Modifier
                    .padding(16.dp)
                    .animateContentSize(),
                action = {
                    customVisuals.actionLabel?.let { label ->
                        TextButton(onClick = { data.performAction() }) {
                            Text(
                                text = label,
                                color = Color.White,
                                fontWeight = FontWeight.Bold
                            )
                        }
                    }
                },
                dismissAction = {
                    if (customVisuals.withDismissAction) {
                        IconButton(onClick = { data.dismiss() }) {
                            Icon(
                                imageVector = Icons.Default.Close,
                                contentDescription = "关闭",
                                tint = Color.White
                            )
                        }
                    }
                },
                dismissActionContentColor = Color.White,
                shape = RoundedCornerShape(8.dp),
                containerColor = bgColor,
                contentColor = Color.White,
                actionContentColor = Color.White
            ) {
                Row(
                    verticalAlignment = Alignment.CenterVertically,
                    horizontalArrangement = Arrangement.spacedBy(12.dp)
                ) {
                    // 图标
                    Icon(
                        imageVector = icon,
                        contentDescription = null,
                        modifier = Modifier.size(24.dp)
                    )

                    // 文本
                    Text(
                        text = customVisuals.message,
                        style = MaterialTheme.typography.bodyMedium,
                        maxLines = 3,
                        overflow = TextOverflow.Ellipsis
                    )
                }
            }
        }
    )
}
